﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MonoTouchProjectsHandler.cs" company="Roman '4ux-nbIx' Novitsky">
//   Copyright (c) 2012 Roman '4ux-nbIx' Novitsky
// </copyright>
// <summary>
//   Defines the MonoTouchProjectsHandler type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace MonoDevAssist
{
  #region Namespace Imports

  using System.Linq;
  using System.Xml.Linq;

  using EnvDTE;

  using Microsoft.VisualStudio;
  using Microsoft.VisualStudio.Shell.Interop;

  #endregion


  internal sealed class MonoTouchProjectsHandler : IVsSolutionEvents
  {
    #region Public Methods

    public int OnAfterCloseSolution(object reserved)
    {
      return VSConstants.S_OK;
    }


    public int OnAfterLoadProject(IVsHierarchy stubHierarchy, IVsHierarchy realHierarchy)
    {
      return VSConstants.S_OK;
    }


    public int OnAfterOpenProject(IVsHierarchy hierarchy, int added)
    {
      object projectObj;
      hierarchy.GetProperty(VSConstants.VSITEMID_ROOT, (int)__VSHPROPID.VSHPROPID_ExtObject, out projectObj);
      var project = (Project)projectObj;

      if (project.IsMonoTouchProject())
      {
        UpdateTargetFramework(project.FileName);

        Utilities.CheckMonoTouchFramework();
      }

      return VSConstants.S_OK;
    }


    public int OnAfterOpenSolution(object reserved, int newSolution)
    {
      Utilities.CheckMonoTouchFramework();

      return VSConstants.S_OK;
    }


    public int OnBeforeCloseProject(IVsHierarchy hierarchy, int removed)
    {
      return VSConstants.S_OK;
    }


    public int OnBeforeCloseSolution(object reserved)
    {
      return VSConstants.S_OK;
    }


    public int OnBeforeUnloadProject(IVsHierarchy realHierarchy, IVsHierarchy stubHierarchy)
    {
      return VSConstants.S_OK;
    }


    public int OnQueryCloseProject(IVsHierarchy hierarchy, int removing, ref int cancel)
    {
      return VSConstants.S_OK;
    }


    public int OnQueryCloseSolution(object reserved, ref int cancel)
    {
      return VSConstants.S_OK;
    }


    public int OnQueryUnloadProject(IVsHierarchy realHierarchy, ref int cancel)
    {
      return VSConstants.S_OK;
    }

    #endregion


    #region Methods

    private void UpdateTargetFramework(string fileName)
    {
      try
      {
        XDocument document = XDocument.Load(fileName);

        const string monoTouchCommonTargetsPath = @"$(ProgramFiles)\MSBuild\MonoTouch\Novell.MonoTouch.Common.targets";

        var importElement = (from element in document.Descendants()
                             where
                               element.Name.LocalName == "Import"
                               && (string)element.Attribute("Project") == monoTouchCommonTargetsPath
                             select element).FirstOrDefault();

        if (importElement != null)
        {
          return;
        }

        var targetFrameworkVersionElement = (from element in document.Descendants()
                             where
                               element.Name.LocalName == "TargetFrameworkVersion"
                             select element).FirstOrDefault();

        if (targetFrameworkVersionElement != null)
        {
          targetFrameworkVersionElement.Remove();
        }

        var targetFrameworkProfileElement = (from element in document.Descendants()
                                             where
                                               element.Name.LocalName == "TargetFrameworkProfile"
                                             select element).FirstOrDefault();

        if (targetFrameworkProfileElement != null)
        {
          targetFrameworkProfileElement.Remove();
        }

        importElement = (from element in document.Descendants()
                         where
                           element.Name.LocalName == "Import"
                           && (string)element.Attribute("Project") == @"$(MSBuildBinPath)\Microsoft.CSharp.targets"
                         select element).FirstOrDefault();

        if (importElement == null)
        {
          return;
        }

        var monoTouchTargetsImport = new XElement(XName.Get("Import", importElement.Name.NamespaceName));
        monoTouchTargetsImport.Add(new XAttribute("Project", monoTouchCommonTargetsPath));
        monoTouchTargetsImport.Add(new XAttribute("Condition", @"'$(windir)' != '' "));
        importElement.AddBeforeSelf(monoTouchTargetsImport);

        document.Save(fileName);
      }
      catch
      {
        return;
      }
    }

    #endregion
  }
}